package irri.soils.ssnm;

import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TableRow;
import android.widget.TextView;

public class ActivityY extends SSNM implements TextWatcher{
	private Bundle b;
	private NoImeEditText f1;
	private NoImeEditText f2;
	private NoImeEditText f3;
	private NoImeEditText f4;
	private NoImeEditText w1;
	private NoImeEditText w2;
	private NoImeEditText w3;
	private NoImeEditText w4;
	private NoImeEditText[] bagText; 
	private NoImeEditText[] kgText;
    private int[] bag = new int[4];
	private int[] kg = new int[4];
	private TableRow tableRow4;
	private TableRow tableRow5;
	private TextView textView1;
	private TextView textView2;
	private TextView textView3;
	private TextView textView4;
	private TextView textView5;
	private TextView textView6;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.y);
        setCustomTitle();
        TextView question1 = (TextView) findViewById(R.id.question1);
        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView3 = (TextView) findViewById(R.id.textView3);
        textView4 = (TextView) findViewById(R.id.textView4);
        textView5 = (TextView) findViewById(R.id.textView5);
        textView6 = (TextView) findViewById(R.id.textView6);
        question1.setText(get("oorganic4"));
        textView1.setText(get("oorganic5"));
        textView2.setText(get("oorganic6"));
        textView3.setText(get("oorganic7"));
        textView4.setText(get("oorganic8"));
        textView5.setText(get("oorganic9"));
        textView6.setText(get("oorganic10"));
        
        b = this.getIntent().getExtras();
        double ha = b.getDouble("ha");
        
        question1.setText(""+question1.getText()+" "+df.format(ha)+" "+get("oorganic11")+".");
                
        f1 = (NoImeEditText) findViewById(R.id.f1);
        f2 = (NoImeEditText) findViewById(R.id.f2);
        f3 = (NoImeEditText) findViewById(R.id.f3);
        f4 = (NoImeEditText) findViewById(R.id.f4);
        w1 = (NoImeEditText) findViewById(R.id.w1);
        w2 = (NoImeEditText) findViewById(R.id.w2);
        w3 = (NoImeEditText) findViewById(R.id.w3);
        w4 = (NoImeEditText) findViewById(R.id.w4);
        tableRow4 = (TableRow) findViewById(R.id.tableRow4);
        tableRow5 = (TableRow) findViewById(R.id.tableRow5);
        
        NoImeEditText[] temp = {f1, f2, f3, f4};
        bagText = temp;
        NoImeEditText[] temp2 = {w1, w2, w3, w4};
        kgText = temp2;
        
        for(int i=0; i<2; i++){
        	bagText[i].toHide(tableRow4);
            bagText[i].toHide(tableRow5);
            kgText[i].toHide(tableRow4);
            kgText[i].toHide(tableRow5);
        }
        
        for(int i=0; i<4; i++){
        	bag[i] = b.getInt("oorgbag"+i);
        	bagText[i].addTextChangedListener(this);
        	kgText[i].addTextChangedListener(this);
        }
        for(int i=0; i<4; i++) 
        	bagText[i].setText(bag[i]==0 ? "" : (""+bag[i]));
        
        for(int i=0; i<4; i++) kg[i] = b.getInt("oorgKg"+i);
        for(int i=0; i<4; i++) 
        	kgText[i].setText(kg[i]==0 ? "" : (""+kg[i]));
        
        mInputView = (KeyboardView) findViewById(R.id.keyboard);
        
        mInputView.setOnKeyboardActionListener(this);
        mSymbolsKeyboard = new LatinKeyboard(this, R.xml.fraction);
        mInputView.setKeyboard(mSymbolsKeyboard);
    }
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityY.this, ActivityX.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
	}
	
	/* (non-Javadoc)
	 * @see irri.soils.ssnm.SSNMActivity#onKey(int, int[])
	 */
	@Override
	public void onKey(int primaryCode, int[] keyCodes) {
		View v = this.getCurrentFocus();
		try{
			TextView tv = (TextView) v;
			String str = ""+tv.getText();
			
			if (primaryCode == Keyboard.KEYCODE_DELETE) {
				onDelete(tv, str);
			}
			else if(primaryCode == 10){
				if(tv.getId()==kgText[1].getId()){
					tableRow4.setVisibility(View.VISIBLE);
					tableRow5.setVisibility(View.VISIBLE);
				}
				for(int i=0; i<4; i++){
					if(tv.getId()==bagText[i].getId()){
						kgText[i].requestFocus(); return;
					}
					else if(tv.getId()==kgText[i].getId()&&i<3){
						bagText[i+1].requestFocus();
						return;
					}
				}
				Intent intent = new Intent(ActivityY.this, ActivityS.class);
				for(int i=0; i<4; i++){
					try{
						bag[i] = Integer.parseInt(""+bagText[i].getText());
					}catch(Exception ex){
						bag[i] = 0;
					}
				}
				for(int i=0; i<4; i++){
					try{
						kg[i] = Integer.parseInt(""+kgText[i].getText());
					}catch(Exception ex){
						kg[i] = 0;
					}
				}
				for(int i=0; i<4; i++)
					if(bag[i]!=0&&kg[i]==0||bag[i]==0&&kg[i]!=0){
						return;
					}
				{
					int i=0;
					for(; i<4; i++)
						if(bag[i]!=0){
							break;
						}
					if(i==4) return;
				}
				for(int i=0; i<4; i++){
					b.putInt("f"+i, bag[i]);
					b.putInt("w"+i, kg[i]);
				}
				intent.putExtras(b);
				startActivity(intent);
				finish();
			}
			else{
				onDigit(tv, str, primaryCode);
			}
		}catch(Exception ex){
			
		}
	}
}